package org.darkforest.task;

import org.darkforest.common.services.GoodsService;
import org.darkforest.common.services.PXianShiService;
import org.darkforest.common.services.PXianshiGoodsService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.CartMapper;
import org.darkforest.model.Goods;
import org.darkforest.model.PXianshi;
import org.darkforest.model.PXianshiGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Libing on 2015/7/8.
 */
@Component
public class ActiveQuartz {

    @Autowired
    private PXianShiService pXianShiService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private PXianshiGoodsService pXianshiGoodsService;

    @Autowired
    private CartMapper cartMapper;





//    @Scheduled(cron = "0 0/2 * * * ?")
    public void recoverActiveGoods(){

        System.out.println("特卖活动结束后 修改特卖活动状态，更新商品价格为非活动价格 每分钟执行一次:" + new Date());
        PXianshi pxianshi = new PXianshi();
        pxianshi.setEndTime(DateUtil.unixTimeStampInt().intValue());
        pxianshi.setActiveState(2); //进行中
        pxianshi.setState((byte)1); //审核正常
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            //查询出已经过期的特卖活动
            List<PXianshi> pXianshiList =pXianShiService.findPXianShiByCondition(pxianshi);
            if(pXianshiList.size()>0) {
                //定义查询条件
                List<Integer> list = new ArrayList<Integer>();
                for (PXianshi pXianshi : pXianshiList) {
                    list.add(pXianshi.getXianshiId());
                }
                map.put("list", list);

                //查询已经过期的特卖活动商品
                List<PXianshiGoods> pXianshiGoodsList = pXianshiGoodsService.batchFindPXianshiGoods(map);
                if(pXianshiGoodsList.size()>0) {
                    for (PXianshiGoods pXianshiGoods : pXianshiGoodsList) {
                        Goods goods = new Goods();
                        goods.setGoodsId(pXianshiGoods.getGoodsId());
                        goods.setGoodsPrice(pXianshiGoods.getGoodsPrice());
                        goods.setGoodsPromotionPrice(new BigDecimal(1)); //折扣率
                        goods.setGoodsPromotionType((byte) 0);
                        goods.setGoodsType(0);
                        //修改已经过期的特卖商品价格为正常价格
                        goodsService.updateByPrimaryKeySelective(goods);


                        //根据GOODS_ID已经加入购车内的商品价格改成折扣价格
                        Map condition = new HashMap();
                        condition.put("goodsId",pXianshiGoods.getGoodsId());
                        condition.put("goodsPrice",pXianshiGoods.getGoodsPrice());
                        condition.put("activeType","0");
                        condition.put("activeId",null);
                        cartMapper.updateGoodsPriceAfterChangeGoodsType(condition);




                    }
                    //修改已经过期的特卖活动状态
                    map.put("state",2);
                    map.put("activeState",3) ;//活动结束
                    pXianShiService.updatePXianShiStateBatch(map);
                    //修改已经过期的特卖活动商品状态
                    pXianshiGoodsService.updatePXianshiStatePriceBatch(map);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 启动限时折扣活动 修改商品价格
     */
//    @Scheduled(cron = "0 0/2 * * * ?")
    public void startXianshiActive(){

        System.out.println("检查正常的限时折扣活动，更新商品价格为活动价格 每分钟执行一次:" + new Date());
        PXianshi pxianshi = new PXianshi();
        pxianshi.setStartTime(DateUtil.unixTimeStampInt().intValue());
        pxianshi.setActiveState(1); //活动就绪状态
        pxianshi.setState((byte)1); //审核正常
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            //查询出已经过期的特卖活动
            List<PXianshi> pXianshiList =pXianShiService.findPXianShiByCondition(pxianshi);
            System.out.println("本次启动限时折扣活动数量:" + pXianshiList.size());

            if(pXianshiList.size()>0) {
                //定义查询条件
                List<Integer> list = new ArrayList<Integer>();
                for (PXianshi pXianshi : pXianshiList) {
                    list.add(pXianshi.getXianshiId());
                }
                map.put("list", list);

                //查询特卖活动商品
                List<PXianshiGoods> pXianshiGoodsList = pXianshiGoodsService.batchFindPXianshiGoods(map);
                if(pXianshiGoodsList.size()>0) {
                    for (PXianshiGoods pXianshiGoods : pXianshiGoodsList) {
                        Goods goods = new Goods();
                        goods.setGoodsId(pXianshiGoods.getGoodsId());
                        goods.setGoodsPrice(pXianshiGoods.getXianshiPrice());

                        goods.setGoodsPromotionPrice(pXianshiGoods.getXianshiPrice().divide(pXianshiGoods.getGoodsPrice())); //折扣率
                        goods.setGoodsPromotionType((byte) 2);
                        goods.setGoodsType(1);

                        //修改已经过期的特卖商品价格为正常价格
                        goodsService.updateByPrimaryKeySelective(goods);
                        //根据GOODS_ID已经加入购车内的商品价格改成折扣价格
                        Map condition = new HashMap();
                        condition.put("goodsId", pXianshiGoods.getGoodsId());
                        condition.put("goodsPrice", pXianshiGoods.getXianshiPrice());
                        condition.put("activeType", "2");
                        condition.put("activeId", pXianshiGoods.getXianshiId());
                        cartMapper.updateGoodsPriceAfterChangeGoodsType(condition);



                    }
                    //修改特卖活动状态
                    map.put("state",1);
                    map.put("activeState",2) ;//活动开始
                    pXianShiService.updatePXianShiStateBatch(map);
                    //修改特卖活动商品状态
                    pXianshiGoodsService.updatePXianshiStatePriceBatch(map);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }



    }


}
